﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Reflection;
using Newtonsoft.Json;
using System.Text;
using Newtonsoft.Json.Linq;
using System.Threading.Tasks;
using System.IO;
using System.Xml.Linq;
using Ymatou.API.Common;

namespace YmatouAPI.Test
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //
                loadApiTypes();
            }
        }
        void loadApiTypes()
        {
            txtApiTypes.Items.Add(new ListItem() { Text = "请选择相关服务接口", Value = string.Empty });
            ///
            foreach (var entity in ApiConfigs.ApiEntitys)
            {
                txtApiTypes.Items.Add(new ListItem()
                {
                    Text = string.Format("{0}【{1}】", entity.ApiName, entity.DllName),
                    Value = entity.DllName
                });
            }
        }
       
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_ok_Click(object sender, EventArgs e)
        {
            //return;
            string strUrl = txtServiceUrl.Text.Trim();
            string strJsonRequest = txt_request.Text.Trim();
            string strApiName = txt_apiname.Text.Trim();
            Boolean boolHttpGet = true;
            if (txt_httpget.Text.Trim() == "HttpPost")
            {
                boolHttpGet = false;
            }
            var requestName = txt_apis.SelectedValue.Trim();

            var client = new DefaultClient(strUrl);
            try
            {
                var responseModel = client.Execute(strApiName, strJsonRequest, boolHttpGet);
                txt_response.Text = JsonConvert.SerializeObject(responseModel, Formatting.Indented);
            }
            catch (Exception ex)
            {
                txt_response.Text = ex.Message;
            }
        }
        /// <summary>
        /// 选择具体服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txt_apis_SelectedIndexChanged(object sender, EventArgs e)
        {
            var dllname=this.txtApiTypes.SelectedValue.Trim();
             var apiutil = ApiConfigs.ApiDictionarys.FirstOrDefault(key => key.Key == dllname);
           
            DropDownList ddl = (DropDownList)sender;//获得DropDownList1控件
            var requestName = ddl.SelectedValue.Trim();

            foreach (var t in apiutil.Value.ListAllRequest)
            {
                if (t.RequestType.FullName == requestName)
                {
                    var temp_class = t.RequestType.CreateDefaultObj();
                    txt_request.Text = JsonConvert.SerializeObject(temp_class, Formatting.Indented);
                    txt_api_desc.Text = t.Summary;
                    foreach (var field in temp_class.GetType().GetProperties())
                    {
                        if (field.Name == "ApiName")
                        {
                            txt_apiname.Text = field.GetValue(temp_class, null).ToString();
                            continue;
                        }
                        if (field.Name == "HttpGet")
                        {
                            if (bool.Parse(field.GetValue(temp_class, null).ToString()))
                            {
                                txt_httpget.Text = "HttpGet";
                            }
                            else
                            {
                                txt_httpget.Text = "HttpPost";
                            }
                            continue;
                        }

                    }
                    break;
                }
            }
            txt_response.Text = string.Empty;
        }
        /// <summary>
        /// 选择Api服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtApiTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.txt_apis.Items.Clear();
         
            DropDownList ddl = (DropDownList)sender;//获得DropDownList1控件
            var dllname = ddl.SelectedValue.Trim();
            txtServiceUrl.Text = ApiConfigs.ApiEntitys.FirstOrDefault(a => a.DllName == dllname).ApiUrl_Alpha;
            var apiutil = ApiConfigs.ApiDictionarys.First(key => key.Key == dllname);
            //
            int icount = 0;
            var group01 = apiutil.Value.ListAllRequest.GroupBy(m => new { RequestNamespace = m.RequestNamespace });
            foreach (var group01_child in group01)
            {
                txt_apis.Items.Add(new ListItem { Text = group01_child.Key.RequestNamespace, Value = "optgroup" });
                //
                foreach (var t in group01_child.OrderBy(t1 => t1.RequestType.Name))
                {
                    //
                    if (icount == 0)
                    {
                        var li_temp = new ListItem
                        {
                            Text = t.RequestType.Name,
                            Value = t.RequestType.FullName,
                            Selected = true
                        };

                        txt_apis.Items.Add(li_temp);
                        //
                        var temp_class = t.RequestType.CreateDefaultObj();
                        txt_request.Text = JsonConvert.SerializeObject(temp_class, Formatting.Indented);
                        txt_api_desc.Text = t.Summary;

                        foreach (var field in temp_class.GetType().GetProperties())
                        {
                            if (field.Name == "ApiName")
                            {
                                txt_apiname.Text = field.GetValue(temp_class, null).ToString();
                                continue;
                            }
                            if (field.Name == "HttpGet")
                            {
                                if (bool.Parse(field.GetValue(temp_class, null).ToString()))
                                {
                                    txt_httpget.Text = "HttpGet";
                                }
                                else
                                {
                                    txt_httpget.Text = "HttpPost";
                                }
                                continue;
                            }

                        }
                        icount = 100;
                    }
                    else
                    {
                        txt_apis.Items.Add(new ListItem
                        {
                            Text = t.RequestType.Name,
                            Value = t.RequestType.FullName
                        });
                    }
                }
            }
            //

        }
        /// <summary>
        /// 更新服务包
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_update_dll_Click(object sender, EventArgs e)
        {

        }
    }
}